from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import RedirectResponse

from .database import engine, Base
from .api.v1 import admin, proxy

# 创建数据库表
Base.metadata.create_all(bind=engine)

app = FastAPI(
    title="AIProxy",
    description="一个用于管理和代理Gemini API请求的系统。",
    version="0.1.0",
)

@app.middleware("http")
async def log_headers(request: Request, call_next):
    print(f"Request headers: {request.headers}")
    response = await call_next(request)
    return response

# 挂载静态文件目录
app.mount("/static", StaticFiles(directory="aiproxy/static"), name="static")

# 包含API路由
app.include_router(admin.router, prefix="/api/v1/admin", tags=["Admin"])
app.include_router(proxy.router, prefix="/api/v1/proxy", tags=["Proxy"])

@app.get("/")
def read_root():
    return RedirectResponse(url="/static/index.html")
